package goleetcode

func longestEqualSubarray(nums []int, k int) int {
	ans := 0
	positions := make([][]int, len(nums)+1)
	for i, x := range nums {
		positions[x] = append(positions[x], i-len(positions[x]))
	}
	for _, pos := range positions {
		if len(pos) <= ans {
			continue
		}
		left := 0
		for right, p := range pos {
			for p-pos[left] > k {
				left++
			}
			ans = max(ans, right-left+1)
		}
	}
	return ans
}


